$(function() {
	var id = $("#pId").val();
	$.post("viewPermissionfindMen.do?id=" + id, function(data) {
		var html = "";
		for (var i = 0; i < data.data.length; i++) {
			html += "<span>" + data.data[i].mNames + "</span>";
		}
		$("#menusub").html(html);
	}, "json")
	$.post("viewPermissionfindSub.do?id=" + id, function(data) {
		var html = "";
		for (var i = 0; i < data.data.length; i++) {
			html += "<span>" + data.data[i].sPathName + "</span>";
		}
		$("#sub").html(html);
	}, "json")
})

layui.use([ 'form', 'layer' ], function() {
	$ = layui.jquery;
	var form = layui.form, layer = layui.layer;

	//监听提交
	form.on('submit(add)', function(data) {
		var arr = new Array();
		var arr2 = new Array();
		$("input:checkbox[name='checkbox2']:checked").each(function(i) {
			arr[i] = $(this).val();
		});
		$("input:checkbox[name='checkbox1']:checked").each(function(i) {
			arr2[i] = $(this).val();
		});
		data.field.checkbox2 = arr.join(",");//将数组合并成字符串
		data.field.checkbox1 = arr2.join(",");//将数组合并成字符串
		$.post("modifyAuthority.do", {
			pMainId : data.field.checkbox1,
			pSubId : data.field.checkbox2,
			id : $("#pId").val()
		}, function(res) {
			if (res.type == 1) {
				layer.alert("修改成功", {
					icon : 6
				}, function() {
					// 获得frame索引
					window.parent.location.reload();
					var index = parent.layer.getFrameIndex(window.name);
					//关闭当前frame
					window.location.reload();
					parent.layer.close(index);
				});
			} else {
				window.location.reload();
				layer.msg(data.msg, {
					area : [ '300px', '200px' ]
				});
			}
		}, "json")
		return false;
	});

	menu();
	sub();
	function menu() {
		$.post("menu.do", function(data) {
			var html = "";
			for (var i = 0; i < data.data.length; i++) {
				html += '<input type="checkbox" name="checkbox1" title="'+data.data[i].mNames+'" value="'
						+ data.data[i].mId+'">';
			}
			$("#menusub2").html(html);

		}, "json")
	}

	function sub() {
		$.post("sub.do", function(data) {
			var html = "";
			for (var i = 0; i < data.data.length; i++) {
				html += " <input type='checkbox' name='checkbox2' title='"+data.data[i].sPathName+"' value='"+data.data[i].sId +"'>";
			}
			$("#sub2").html(html);
		}, "json")
	}

});